﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace BskLeague.Models
{
    public static class PdfGenerator
    {
        public static void GenerateSchedule(string Filename, string path, string PlaceAndDate, string Address, string Main, string Introduction, string Ending, string Sign, List<Match> MatchesToPrint)
        {
            var doc1 = new Document();
            var normalFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, 12, Font.NORMAL);
            var bigFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, 18, Font.NORMAL);
            
            PdfWriter.GetInstance(doc1, new FileStream(path + "/" + Filename + ".pdf", FileMode.Create));
            doc1.Open();
            Paragraph p1 = new Paragraph(PlaceAndDate, normalFont);
            p1.Alignment = 2;
            Paragraph p2 = new Paragraph(Address + "\n\n", normalFont);
            p2.Alignment = 0;
            Paragraph p3 = new Paragraph(Main + "\n\n", bigFont);
            p3.Alignment = 1;
            Paragraph p4 = new Paragraph(Introduction + "\n\n", normalFont);
            p4.Alignment = 0;
            Paragraph p5 = new Paragraph(Ending + "\n\n", normalFont);
            p5.Alignment = 0;
            Paragraph p6 = new Paragraph(Sign, normalFont);
            p6.Alignment = 2;

            PdfPTable table = new PdfPTable(5);
            table.AddCell(new Phrase("Data", normalFont));
            table.AddCell(new Phrase("Runda", normalFont));
            table.AddCell(new Phrase("Drużyna 1", normalFont));
            table.AddCell(new Phrase("Drużyna 2", normalFont));
            table.AddCell(new Phrase("Miejsce", normalFont));
            if (MatchesToPrint.Count > 0)
            {
                foreach (var m in MatchesToPrint)
                {

                    table.AddCell(new Phrase(m.Date.ToString(), normalFont));
                    table.AddCell(new Phrase(m.Round.ToString(), normalFont));
                    table.AddCell(new Phrase(m.Team1.TeamName, normalFont));
                    table.AddCell(new Phrase(m.Team2.TeamName, normalFont));
                    table.AddCell(new Phrase(m.Place.stadiumName, normalFont));
                }
            }

            doc1.Add(p1);
            doc1.Add(p2);
            doc1.Add(p3);
            doc1.Add(p4);
            doc1.Add(table);
            doc1.Add(p5);
            doc1.Add(p6);
            doc1.Close();

        }

        public static void GenerateAccount(string Filename, string path, string PlaceAndDate, string Main, string Introduction, string UserText, string PasswordText)
        {
            var doc1 = new Document();
            var normalFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, 12, Font.NORMAL);
            var boldFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, 12, Font.BOLD);
            var bigFont = FontFactory.GetFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, 22, Font.NORMAL);

            PdfWriter.GetInstance(doc1, new FileStream(path + "/" + Filename + ".pdf", FileMode.Create));
            doc1.Open();
            Paragraph p1 = new Paragraph(PlaceAndDate, normalFont);
            p1.Alignment = 2;
           
            Paragraph p2 = new Paragraph(Main + "\n\n", bigFont);
            p2.Alignment = 1;
            Paragraph p3 = new Paragraph(Introduction + "\n\n", normalFont);
            p3.Alignment = 1;
            Paragraph p4 = new Paragraph(UserText + "\n\n", boldFont);
            p4.Alignment = 0;
            Paragraph p5 = new Paragraph(PasswordText, boldFont);
            p5.Alignment = 0;
            Paragraph p6 = new Paragraph("Z wyrazami szacunku", normalFont);
            p6.Alignment = 2;
            Paragraph p7 = new Paragraph("Administrator", normalFont);
            p7.Alignment = 2;
            doc1.Add(p1);
            doc1.Add(p2);
            doc1.Add(p3);
            doc1.Add(p4);
            doc1.Add(p5);
            doc1.Add(p6);
            doc1.Add(p7);
            doc1.Close();

        }
    }
}